package utils

import (
	"bytes"
	"golang.org/x/text/encoding/simplifiedchinese"
	"golang.org/x/text/transform"
	"io"
	"io/ioutil"
)

// UTF8ReDecode UTF8 转换GBK
func UTF8ReDecode(text string, encodeStr string) string {
	var reader io.Reader
	switch encodeStr {
	case "GB18030":
		reader = transform.NewReader(bytes.NewReader([]byte(text)), simplifiedchinese.GB18030.NewDecoder())
	case "GBK":
		reader = transform.NewReader(bytes.NewReader([]byte(text)), simplifiedchinese.GBK.NewDecoder())
	case "HZGB2312":
		reader = transform.NewReader(bytes.NewReader([]byte(text)), simplifiedchinese.HZGB2312.NewDecoder())
	default:
		reader = transform.NewReader(bytes.NewReader([]byte(text)), simplifiedchinese.GB18030.NewDecoder())
	}
	d, err := ioutil.ReadAll(reader)
	if err != nil {
		return ""
	}
	return string(d)
}
